package com.example.comment.mq.listener;

import com.alibaba.fastjson.JSON;
import com.example.comment.base.dto.LikeDTO;
import com.example.comment.service.CommentLikeService;
import com.example.comment.utils.SystemException;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * @author QingZeyu
 * @date 2024/5/9 13:22
 */
@Component

@Slf4j
public class CommentMqHandler {
    @Resource
    private CommentLikeService commentLikeService;
    @RabbitListener(queues = "${mq-config.queue}")
    public void handle(String msg, Message message, Channel channel) {
        LikeDTO dto = JSON.parseObject(msg, LikeDTO.class);
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), Boolean.FALSE);
            commentLikeService.like(dto);
        } catch (Exception e) {
            log.error("点赞业务:{}",e);
            throw new SystemException(Optional.ofNullable(e.getMessage()).orElse("点赞业务处理系统异常"));
        }
    }
}
